package cn.xinfei.xdecision.datasource.mysql;

import cn.xinfei.xdecision.datasource.mysql.dynamic.DynamicDatasource;
import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Slf4j
@Configuration
@ConditionalOnProperty( name = "spring.datasource.druid.data.enable")
@MapperScan(basePackages = {"cn.xinfei.xdecision.common.datadao.**"},sqlSessionTemplateRef = "sqlSessionTemplateData")
public class DataMySQLConfiguration {


    @Autowired
    private DefaultDBFieldHandler defaultDBFieldHandler;


    @Bean(name = "druidDataSourceData")
    @ConditionalOnProperty( name = "spring.datasource.druid.data.enable")
    @ConfigurationProperties(prefix = "spring.datasource.druid.data")
    public DruidDataSource druidDataSourceData() {
        log.info("初始化 druidDataSourceData");
        return new DruidDataSource();
    }

    @Bean(name = "sqlSessionFactoryData")
    @ConditionalOnProperty( name = "spring.datasource.druid.data.enable")
    public SqlSessionFactory sqlSessionFactoryData(@Qualifier("druidDataSourceData") DruidDataSource druidDataSourceData) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] mapperXmlResource = resolver.getResources("classpath*:cn/xinfei/xdecision/common/datadao/*Mapper.xml");
        bean.setMapperLocations(mapperXmlResource);
        bean.setDataSource(druidDataSourceData);
        log.info("druidDataSourceData source.db={}",druidDataSourceData);
        bean.setTypeAliasesPackage("cn.xinfei.xdecision.common.datamodel");
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setMetaObjectHandler(defaultDBFieldHandler);
        bean.setGlobalConfig(globalConfig);
        MybatisConfiguration configuration = new MybatisConfiguration();
        configuration.setMapUnderscoreToCamelCase(true);
        configuration.setCacheEnabled(false);
        bean.setConfiguration(configuration);
        return bean.getObject();
    }


    @Bean(name = "transactionManagerData")
    @ConditionalOnProperty( name = "spring.datasource.druid.data.enable")
    public DataSourceTransactionManager transactionManagerData(@Qualifier("druidDataSourceData") DruidDataSource druidDataSourceData) {
        DataSourceTransactionManager dm = new DataSourceTransactionManager();
        dm.setDataSource(druidDataSourceData);
        return dm;
    }


    @Bean(name = "sqlSessionTemplateData")
    @ConditionalOnProperty( name = "spring.datasource.druid.data.enable")
    public SqlSessionTemplate sqlSessionTemplateData(@Qualifier("sqlSessionFactoryData") SqlSessionFactory sqlSessionFactoryData) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryData);
    }


}